package com.lanyou.cook.web;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.datadictionary.KettleJobScheduleStatus;
import com.lanyou.cook.datadictionary.UserLogOpt;
import com.lanyou.cook.entity.domain.InterfaceSchedule;
import com.lanyou.cook.entity.domain.InterfaceScheduleServer;
import com.lanyou.cook.entity.kettle.KettleSlaveServer;
import com.lanyou.cook.repository.InterfaceScheduleRepository;
import com.lanyou.cook.repository.KettleSlaveServerRepository;
import com.lanyou.cook.service.UserLogService;
import com.lanyou.cook.web.param.UpdateScheduleServerParam;


/**
 * 接口调度管理相关业务控制层
 * @author 曾族根
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/interfaceSchedules")
public class InterfaceScheduleController2 {

	@Autowired
	private InterfaceScheduleRepository interfaceScheduleRepository;
	
	@Autowired
	private KettleSlaveServerRepository kettleSlaveServerRepository;
	
	@Autowired
	private UserLogService userLogService;
	
	
	/**
	 * 修改调度服务器信息
	 * @param param	类型:UpdateScheduleServerParam 待更新的调度服务器参数实体
	 */
	@RequestMapping(value = "/updateScheduleServer", method=RequestMethod.POST)
	@RequiresPermissions(value={"interfaceServiceSettings:maintain","interfaceSchedulingManagement:view"},logical=Logical.OR)
	@Transactional
	public void updateScheduleServer(@RequestBody UpdateScheduleServerParam param) {
		InterfaceSchedule interfaceSchedule = interfaceScheduleRepository.findOne(InterfaceSchedule.idSpecification(param.getId()));
		if(param.getSlaveServerId() != null){
			KettleSlaveServer slaveServer = kettleSlaveServerRepository.findOne(param.getSlaveServerId());
			InterfaceScheduleServer scheduleServer = new InterfaceScheduleServer();
			scheduleServer.setRunWay(param.getRunWay());
			scheduleServer.setSlaveServer(slaveServer);
			interfaceSchedule.setScheduleServer(scheduleServer);
		}else{
			InterfaceScheduleServer scheduleServer = new InterfaceScheduleServer();
			scheduleServer.setRunWay(param.getRunWay());
			interfaceSchedule.setScheduleServer(scheduleServer);
		}
		interfaceScheduleRepository.save(Arrays.asList(interfaceSchedule));
		
		userLogService.save(UserLogOpt.MaintenanceInterfaceServiceSetting, null, String.format("修改了调度“%s”的执行服务器设置", interfaceSchedule.getName()));
	}
	
	
	/**
	 *  改变指定调度的状态
	 * @param id 类型:String 待改变的调度ID
	 * @param status 类型:String 状态值
	 * @return 类型:String 返回调度改变后的新状态值
	 */
	@RequestMapping(value = "/changeScheduleStatus", method=RequestMethod.POST, produces="application/json")
	@Transactional
	public String changeScheduleStatus(String id, String status){
		JsonObject result = new JsonObject();
		InterfaceSchedule interfaceSchedule = interfaceScheduleRepository.findOne(InterfaceSchedule.idSpecification(id));
		KettleJobScheduleStatus newStatus;
		
		if(StringUtils.equalsIgnoreCase(status, KettleJobScheduleStatus.Off.toString())){
			newStatus = KettleJobScheduleStatus.Off;
			
		} else if(StringUtils.equalsIgnoreCase(status, KettleJobScheduleStatus.On.toString())){
			newStatus = KettleJobScheduleStatus.On;
			
		} else {
			if(KettleJobScheduleStatus.Off.ValueCode.equals(interfaceSchedule.getRunStatus())){
				newStatus = KettleJobScheduleStatus.On;
				
			} else {
				newStatus = KettleJobScheduleStatus.Off;
			}
		}
		
		interfaceSchedule.setRunStatus(newStatus.ValueCode);
		interfaceScheduleRepository.save(Arrays.asList(interfaceSchedule));
		
		switch(newStatus){
		case On:
			userLogService.save(UserLogOpt.MaintenanceDispatch, null, String.format("启用调度“%s”", interfaceSchedule.getName()));
			break;
		case Off:
			userLogService.save(UserLogOpt.MaintenanceDispatch, null, String.format("禁用调度“%s”", interfaceSchedule.getName()));
			break;
		}
		
		result.addProperty("nowTheScheduleStatus", newStatus.toString());
		return result.toString();
	}
	
	
}
